﻿using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace HenhouseEnvironmentCtrl
{
    public partial class AddHenhouseForm : Form
    {
        public AddHenhouseForm()
        {
            InitializeComponent();
            SetHintText();
        }

        private void SetHintText()
        {
            SetHint(txtHenhouseName, "请输入鸡舍名称");
            SetHint(txtTempMax, "最大温度（建议范围：0-50）");
            SetHint(txtTempMin, "最小温度（建议范围：0-50）");
            SetHint(txtHumMax, "最大湿度（建议范围：0-100）");
            SetHint(txtHumMin, "最小湿度（建议范围：0-100）");
            SetHint(txtPM25Max, "最大 PM2.5（建议范围：0-500）");
            SetHint(txtPM25Min, "最小 PM2.5（建议范围：0-500）");
            SetHint(txtNH3Max, "最大 NH3（建议范围：0-100）");
            SetHint(txtNH3Min, "最小 NH3（建议范围：0-100）");
            SetHint(txtCO2Max, "最大 CO2（建议范围：0-10000）");
            SetHint(txtCO2Min, "最小 CO2（建议范围：0-10000）");
        }

        private void SetHint(TextBox textBox, string hintText)
        {
            textBox.Text = hintText;
            textBox.ForeColor = Color.Gray;
            textBox.GotFocus += (sender, e) =>
            {
                if (textBox.Text == hintText)
                {
                    textBox.Text = "";
                    textBox.ForeColor = Color.Black;
                }
            };
            textBox.LostFocus += (sender, e) =>
            {
                if (string.IsNullOrWhiteSpace(textBox.Text))
                {
                    textBox.Text = hintText;
                    textBox.ForeColor = Color.Gray;
                }
            };
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateInputs(out string errorMessage))
            {
                MessageBox.Show(errorMessage, "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string henhouseName = txtHenhouseName.Text;
            float tempMax = float.Parse(txtTempMax.Text);
            float tempMin = float.Parse(txtTempMin.Text);
            float humMax = float.Parse(txtHumMax.Text);
            float humMin = float.Parse(txtHumMin.Text);
            int pm25Max = int.Parse(txtPM25Max.Text);
            int pm25Min = int.Parse(txtPM25Min.Text);
            int nh3Max = int.Parse(txtNH3Max.Text);
            int nh3Min = int.Parse(txtNH3Min.Text);
            int co2Max = int.Parse(txtCO2Max.Text);
            int co2Min = int.Parse(txtCO2Min.Text);

            using (SqlConnection conn = DBConnection.CreateConnection())
            {
                DBConnection.Open(conn);
                string checkQuery = "SELECT COUNT(*) FROM Henhouse WHERE HenhouseName = @HenhouseName";
                SqlCommand checkCmd = new SqlCommand(checkQuery, conn);
                checkCmd.Parameters.AddWithValue("@HenhouseName", henhouseName);
                int count = (int)checkCmd.ExecuteScalar();

                if (count > 0)
                {
                    MessageBox.Show("该鸡舍名称已存在，请选择其他名称。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string query = "INSERT INTO Henhouse (HenhouseName, TempMax, TempMin, HumMax, HumMin, PM25Max, PM25Min, NH3Max, NH3Min, CO2Max, CO2Min) " +
                               "VALUES (@HenhouseName, @TempMax, @TempMin, @HumMax, @HumMin, @PM25Max, @PM25Min, @NH3Max, @NH3Min, @CO2Max, @CO2Min)";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@HenhouseName", henhouseName);
                cmd.Parameters.AddWithValue("@TempMax", tempMax);
                cmd.Parameters.AddWithValue("@TempMin", tempMin);
                cmd.Parameters.AddWithValue("@HumMax", humMax);
                cmd.Parameters.AddWithValue("@HumMin", humMin);
                cmd.Parameters.AddWithValue("@PM25Max", pm25Max);
                cmd.Parameters.AddWithValue("@PM25Min", pm25Min);
                cmd.Parameters.AddWithValue("@NH3Max", nh3Max);
                cmd.Parameters.AddWithValue("@NH3Min", nh3Min);
                cmd.Parameters.AddWithValue("@CO2Max", co2Max);
                cmd.Parameters.AddWithValue("@CO2Min", co2Min);
                cmd.ExecuteNonQuery();
                DBConnection.Close(conn);
            }

            MessageBox.Show("鸡舍添加成功！");
            this.Close();
        }

        private bool ValidateInputs(out string errorMessage)
        {
            errorMessage = "";

            if (string.IsNullOrWhiteSpace(txtHenhouseName.Text) || txtHenhouseName.Text == "请输入鸡舍名称")
            {
                errorMessage = "鸡舍名称不能为空";
                return false;
            }

            if (!float.TryParse(txtTempMax.Text, out _))
            {
                errorMessage = "请输入有效的最大温度";
                return false;
            }

            if (!float.TryParse(txtTempMin.Text, out _))
            {
                errorMessage = "请输入有效的最小温度";
                return false;
            }

            if (!float.TryParse(txtHumMax.Text, out _))
            {
                errorMessage = "请输入有效的最大湿度";
                return false;
            }

            if (!float.TryParse(txtHumMin.Text, out _))
            {
                errorMessage = "请输入有效的最小湿度";
                return false;
            }

            if (!int.TryParse(txtPM25Max.Text, out _))
            {
                errorMessage = "请输入有效的最大PM2.5值";
                return false;
            }

            if (!int.TryParse(txtPM25Min.Text, out _))
            {
                errorMessage = "请输入有效的最小PM2.5值";
                return false;
            }

            if (!int.TryParse(txtNH3Max.Text, out _))
            {
                errorMessage = "请输入有效的最大NH3值";
                return false;
            }

            if (!int.TryParse(txtNH3Min.Text, out _))
            {
                errorMessage = "请输入有效的最小NH3值";
                return false;
            }

            if (!int.TryParse(txtCO2Max.Text, out _))
            {
                errorMessage = "请输入有效的最大CO2值";
                return false;
            }

            if (!int.TryParse(txtCO2Min.Text, out _))
            {
                errorMessage = "请输入有效的最小CO2值";
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
